{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "YNLwNM6nBgYj"
   },
   "source": [
    "<center>\n",
    "    <p style=\"text-align:center\">\n",
    "        <img alt=\"phoenix logo\" src=\"https://raw.githubusercontent.com/Arize-ai/phoenix-assets/9e6101d95936f4bd4d390efc9ce646dc6937fb2d/images/socal/github-large-banner-phoenix.jpg\" width=\"1000\"/>\n",
    "        <br>\n",
    "        <br>\n",
    "        <a href=\"https://arize.com/docs/phoenix/\">Docs</a>\n",
    "        |\n",
    "        <a href=\"https://github.com/Arize-ai/phoenix\">GitHub</a>\n",
    "        |\n",
    "        <a href=\"https://arize-ai.slack.com/join/shared_invite/zt-2w57bhem8-hq24MB6u7yE_ZF_ilOYSBw#/shared-invite/email\">Community</a>\n",
    "    </p>\n",
    "</center>\n",
    "<h1 align=\"center\">Tracing Smolagents with Arize Phoenix</h1>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "UAn1qp84jeTe"
   },
   "source": [
    "## Install Dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install smolagents -q\n",
    "!pip install -q arize-phoenix opentelemetry-sdk opentelemetry-exporter-otlp openinference-instrumentation-smolagents"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "r9X87mdGnpbc"
   },
   "source": [
    "## Connect to Phoenix\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "99Qatk6vj2bN"
   },
   "source": [
    "In this example, we'll use Phoenix as our destination. You could instead add any other exporters you'd like in this approach.\n",
    "\n",
    "If you need to set up an API key for Phoenix, you can do so [here](https://app.phoenix.arize.com/)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "GmVK7vV2lzAv"
   },
   "source": [
    "#### Keys\n",
    "\n",
    "Note: For this colab you'll need:\n",
    "\n",
    "*   Phoenix API key\n",
    "*   Hugging Face Token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import getpass\n",
    "import os\n",
    "\n",
    "# Prompt the user for their API keys if they haven't been set\n",
    "if not (phoenix_api_key := os.getenv(\"PHOENIX_API_KEY\")):\n",
    "    phoenix_api_key = getpass.getpass(\"Enter your Phoenix API Key: \")\n",
    "if not (hf_token_value := os.getenv(\"HF_TOKEN\")):\n",
    "    hf_token_value = getpass.getpass(\"Enter your Hugging Face Token: \")\n",
    "\n",
    "# Set the environment variables with the provided keys\n",
    "os.environ[\"PHOENIX_CLIENT_HEADERS\"] = f\"api_key={phoenix_api_key}\"\n",
    "os.environ[\"PHOENIX_COLLECTOR_ENDPOINT\"] = \"https://app.phoenix.arize.com\"\n",
    "os.environ[\"HF_TOKEN\"] = f\"{hf_token_value}\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "LVl2XBq9kMT-"
   },
   "source": [
    "### Define the Trace Provider"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from phoenix.otel import register\n",
    "\n",
    "tracer_provider = register(\n",
    "    project_name=\"my-agent-app\",  # Default is 'default'\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "vYT-EU56ni94"
   },
   "source": [
    "## Instrument Smolagents\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openinference.instrumentation.smolagents import SmolagentsInstrumentor\n",
    "\n",
    "SmolagentsInstrumentor().instrument(tracer_provider=tracer_provider)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "DQWEnbi2k7Sf"
   },
   "source": [
    "## Create your Smolagents & Run\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "UgUAi7F3NP35"
   },
   "source": [
    "Now you can create any sort of agent using the smolagents module, and Phoenix will track every step, input, output, tool, and call your agent makes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from smolagents import (\n",
    "    CodeAgent,\n",
    "    InferenceClientModel,\n",
    "    ToolCallingAgent,\n",
    "    VisitWebpageTool,\n",
    "    WebSearchTool,\n",
    ")\n",
    "\n",
    "model = InferenceClientModel()\n",
    "\n",
    "search_agent = ToolCallingAgent(\n",
    "    tools=[WebSearchTool(), VisitWebpageTool()],\n",
    "    model=model,\n",
    "    name=\"search_agent\",\n",
    "    description=\"This is an agent that can do web search.\",\n",
    ")\n",
    "manager_agent = CodeAgent(\n",
    "    tools=[],\n",
    "    model=model,\n",
    "    managed_agents=[search_agent],\n",
    ")\n",
    "manager_agent.run(\"Based on the latest news, what is happening in extraterrestrial life?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "aOByjYf-XmZp"
   },
   "source": [
    " ## ⭐⭐⭐ If you like this guide, please give [Arize Phoenix](https://github.com/Arize-ai/phoenix) a star ⭐⭐⭐"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
